<?php
class Authencate
{
	public static function Authorize($role)
	{
			
	}

	public static function SignIn($permanent, $userName)
	{
		$text = $user . '/' . $key . '/' . $exp;
		$hash = $user . $key;
		$cookie = $text . '|' . $hash;
		
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$this->_cookie = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $cookie, MCRYPT_MODE_ECB, $iv);
		setcookie('Apollo_AUTHX',$this->_cookie, time()+ $exp, '', '', false, true);
		
	}

	public static function SignOut()
	{
			
	}
	
	private $_cookie;
	private $_arr;
	private $_flag;
	
	// Lay cookie
	private function getCookie(){
		$this->_cookie = $_COOKIE['Apollo_AUTHX'];
		return $this->_cookie;
	}
	
	
	// Tach cookie thanh cac thanh pha
	private function extractCookie($key){
		$cookie = $this->getCookie();
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		$decry = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $cookie, MCRYPT_MODE_ECB,$iv);
	
		$text = explode('|',$decry);
		$data = explode('/', $text[0]);
		array_push($data, $text[1]);
		$this->_arr = $data;
		return $this->_arr;
	}
	
	private function checkLogin($key){
		$arr = $this->extractCookie($key);
		$hash = $arr[0] . $arr[1];
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
		if(strcmp($hash, $arr[3])){
			$this->_flag = true;
		}else{
			$this->_flag = false;
		}
		return $this->_flag;
	}
}
